package com.glela.platform.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.glela.platform.mapper.UserFansMapper;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.User;
import com.glela.platform.service.UserFansService;

@Service("userFansService")
public class UserFansServiceImpl implements UserFansService {

	@Resource
	private UserFansMapper userFansMapper;
	@Resource
	private UserMapper userMapper;

	@Override
	public int attentionAndAbolish(long userId, long talentId, int type) {
		// TODO Auto-generated method stub
		int a;
		if (type == 1) {
			int i = userFansMapper.selectUserFansByUserIdAndTalentID(talentId, userId);
			if (i >0) {
				// 1 已经关注过了
				 a = 1;
				return a;
			}
			userFansMapper.insertUserFans(talentId, userId);
			User user=userMapper.selectUserEntityById(talentId);
			userMapper.updateUserFansCount(user.getFansCount()+1, talentId);
		}
		if (type==0) {
			int b = userFansMapper.selectUserFansByUserIdAndTalentID(talentId, userId);
			if (b < 0) {
				// 0 没有关注过
				a = 0;
				return a;
			}
			userFansMapper.deleteUserFans(talentId, userId);
			User user=userMapper.selectUserEntityById(talentId);
			if (user.getFansCount()>0) {
				userMapper.updateUserFansCount(user.getFansCount()-1, talentId);
			}
		}
		return 5;
	}

}
